// 作业三：随机产生100个整数，数字的范围[-100, 100]，降序输出这些生成的数字并打印其重复的次数
package main

import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)

func main() {
	r := rand.New(rand.NewSource(time.Now().UnixMicro()))
	randNum := make([]int, 0, 101)
	for i := 0; i < 100; i++ {
		randNum = append(randNum, r.Intn(201)-100)
	}
	sort.Sort(sort.Reverse(sort.IntSlice(randNum)))
	fmt.Println(randNum)
	// 实现1
	randNum = append(randNum, -101)
	start := -1
	for i := 0; i < 100; i++ {
		// fmt.Printf("%d %d\n", k, v)
		if randNum[i] == randNum[i+1] && start == -1 {
			start = i
		} else if randNum[i] != randNum[i+1] && start != -1 {
			fmt.Printf("%4d 重复了 %d 次\n", randNum[i], i-start+1)
			start = -1
		} else if randNum[i] != randNum[i+1] && start == -1 {
			fmt.Printf("%4d 重复了 1 次\n", randNum[i])
		}
	}

	//实现2
	// countMap := make(map[int]int, 100)
	// for _, v := range randNum {
	// 	countMap[v] = countMap[v] + 1
	// }
	// fmt.Println()
	// for k, v := range countMap {
	// 	fmt.Printf("%4d 重复了 %d 次\n", k, v)
	// }

}
